package com.yunfeisoft.business.service.impl;

import com.yunfeisoft.business.dao.inter.DonationAssignmentDao;
import com.yunfeisoft.business.dao.inter.DonationAssignmentItemDao;
import com.yunfeisoft.business.model.DonationAssignment;
import com.yunfeisoft.business.service.inter.DonationAssignmentService;
import com.yunfeisoft.common.base.BaseServiceImpl;
import com.yunfeisoft.common.sql.separation.DataSourceChange;
import com.yunfeisoft.common.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Map;

/**
 * ClassName: DonationAssignmentServiceImpl
 * Description: 捐赠物品分配service实现
 * Author: Jackie liu
 * Date: 2020-05-14
 */
@Service("donationAssignmentService")
public class DonationAssignmentServiceImpl extends BaseServiceImpl<DonationAssignment, String, DonationAssignmentDao> implements DonationAssignmentService {

    @Autowired
    private DonationAssignmentItemDao donationAssignmentItemDao;

    @Override
    @DataSourceChange(slave = true)
    public Page<DonationAssignment> queryPage(Map<String, Object> params) {
        return getDao().queryPage(params);
    }

    @Override
    public int save(DonationAssignment donationAssignment) {
        if (!CollectionUtils.isEmpty(donationAssignment.getDonationAssignmentItemList())) {
            donationAssignmentItemDao.batchInsert(donationAssignment.getDonationAssignmentItemList());
        }
        return super.save(donationAssignment);
    }

    @Override
    public int modify(DonationAssignment donationAssignment) {
        donationAssignmentItemDao.deleteByDonationAssignmentId(donationAssignment.getId());
        if (!CollectionUtils.isEmpty(donationAssignment.getDonationAssignmentItemList())) {
            donationAssignmentItemDao.batchInsert(donationAssignment.getDonationAssignmentItemList());
        }
        return super.modify(donationAssignment);
    }
}